﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SEO.Business;
using SEO.Entity;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Configuration;
using mshtml;
using System.Windows.Forms;
using System.Diagnostics;

namespace WebCrawler
{
    /// <summary>
    /// 服务端爬虫主程序
    /// </summary>
    public class CrawlerMain
    {
        /// <summary>
        /// 抓取页数
        /// </summary>
        public static int nPage = 2;

        /// <summary>
        /// 配置信息
        /// </summary>
        public static SysConfig sysInfo = null;

        /// <summary>
        /// 当天的第几次
        /// </summary>
        public static int nSysRunIndex = 1;

        /// <summary>
        /// 运行记录ID
        /// </summary>
        public static int nSysRunLogID = 0;

        /// <summary>
        /// 是否锁定
        /// </summary>
        public static bool SysLock = false;

        /// <summary>
        /// 运行开始时间
        /// </summary>
        public static DateTime dSysBeginTime = DateTime.Now;

        /// <summary>
        /// 总线程数
        /// </summary>
        public static int nAllThreadCount = 0;

        /// <summary>
        /// 每个线程分析的关键字个数
        /// </summary>
        public static int nThreadKeyCount = Convert.ToInt32(ConfigurationManager.AppSettings["ThreadKeyCount"]);

        /// <summary>
        /// 百度url
        /// </summary>
        public static string strBaiduUrl = "http://www.baidu.com/s?ie=utf-8&wd={0}&pn={1}";

        /// <summary>
        /// 百度收入多少天才合要求
        /// </summary>
        public static int nBaiduDay = 2;

        /// <summary>
        /// 是否多线程(0：否,1:是)
        /// </summary>
        public static int nIsThread = Convert.ToInt32(ConfigurationManager.AppSettings["IsThread"]);

        /// <summary>
        /// 访问百度时间
        /// </summary>
        public static DateTime dCheckBaiDuTime = DateTime.Now.AddSeconds(-5);

        public static int BaiduRanNum = 5;


        public static object LockBaidu = new object();
        public static object LockBaidu1 = new object();

        public static object LockBaiduCount = new object();

        public static int CheckBaiDuCount = 0;

        public static void Main(string[] args)
        {
            //清除网站缓存

            // string strUrl = Common.GetFirstUrl("http://www.baidu.com/link?url=Uq8O_ynmDJBcQpMolideh5fMNgpD7sxsn3U707Z7wSgiuTbjnySR961UgOqcCyN9K_7t5aRD67ZsRKf-8eNc5_");
            //  Console.WriteLine(new WebClient().DownloadData("http://yyk.39.net/search/awuhu/ltfuke/c_renxin/").ToString());
           // Console.WriteLine(HtmlTool.GetHtml("http://www.youbian.com/ask/info38145/"));
            //初始化配置信息
            //判断是否有多个程序在运行
            //Process[] cps = Process.GetProcessesByName("WebCrawler");
            //if (cps.Length > 1)
            //{
            //    Console.WriteLine("已有程序在运行");
            //    Common.Log("已有程序在运行");
            //    return;
            //}

            Console.WriteLine("初始化配置信息");
            if (!InitConfig())
            {
                return;
            }

            //主流程
            if (args.Contains("-a"))
            {
                //主流程
                InitMain();
            }
            //分析提成
            if (args.Contains("-m"))
            {
                CrawlerMemonyByDay();
                //CreateKeywordsCount();
            }
            Console.WriteLine("完成");
        }

        /// <summary>
        /// 主流程
        /// </summary>
        public static void InitMain()
        {
            //写入日志
            Console.WriteLine("创建日志");
            if (InserSysRunLog())
            {
                //关键字收录分析
                Console.WriteLine("关键字收录分析");
                //设置关键字分析状态全部为成功
                UpdateKeywordsResult();
                if (nIsThread == 0)
                {
                    //单线程分析
                    CreateAllKeywords();
                }
                else if (nIsThread == 1)
                {
                    //多线程分析
                    CreateKeywordsThread();
                }
                //关键字分析失败的重新分析一次
                CrawlerErrorKeywords();
                //关键字收录表报生成
                Console.WriteLine("关键字收录报表生成");
                CreateKeywordsReport();
                //网编文章收录分析
                Console.WriteLine("网编文章收录分析");
                CrawlerArticle();
                if (nSysRunIndex == 1)
                {
                    //每天7点前锁定关键字
                    if (dSysBeginTime < Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 7:00:00")))
                    {
                        Console.WriteLine("关键字锁定状态分析");
                        CrawlerKeywordsSysLock();
                        //清除网站缓存
                        HtmlTool.GetHtml(ConfigurationManager.AppSettings["WebSite"].ToString() + "/ClearCache.aspx");
                    }
                    Console.WriteLine("用户文章报表生成");
                    CreateUserArticleReport();
                }
                //文章提成月结算
                MainCrawlerMemony();
                //关键字优化量统计
                //CreateKeywordsCount();
                //更新运行日志
                UpdateSysRunLog();
            }
        }

        #region 初始化配置信息
        /// <summary>
        /// 初始化配置信息
        /// </summary>
        public static bool InitConfig()
        {
            try
            {
                sysInfo = new BisSysConfig().GetSysConfig();
                if (sysInfo != null)
                {
                    nPage = sysInfo.BaiduPage.Value;
                    nBaiduDay = sysInfo.BaiduDay.Value;
                }
                else
                {
                    Console.WriteLine("初始化配置信息出错");
                    return false;
                }
                bool bCheckSys = false;
                DataTable dt = new BisMedical().GetList(true);
                if (dt.Rows != null)
                {
                    if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["Keys"].ToString().IndexOf("新安中医院") >= 0)
                            bCheckSys = true;
                    }
                }
                if(!bCheckSys)
                {
                    Console.WriteLine("系统验证出错");
                    Common.Log("系统验证出错");
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("初始化配置信息出错");
                Common.Log(ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 创建日志
        /// </summary>
        /// <returns></returns>
        public static bool InserSysRunLog()
        {
            try
            {
                BisSysRunLog bisSysRunLog = new BisSysRunLog();
                //查找当天最后一条运行记录
                DateTime dBeginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
                DateTime dEndTime = dBeginTime.AddDays(1);
                DataTable dtSysRunLog = bisSysRunLog.GetSysLastLog(dBeginTime, dEndTime);
                if (dtSysRunLog != null && dtSysRunLog.Rows.Count > 0)
                {
                    nSysRunIndex = Convert.ToInt32(dtSysRunLog.Rows[0]["RunIndex"]);
                    nSysRunIndex++;
                }
                else
                {
                    nSysRunIndex = 1;
                }
                dtSysRunLog.Dispose();
                //新增运行记录
                SysRunLog info = new SysRunLog();
                info.BeginTime = dSysBeginTime;
                info.RunIndex = nSysRunIndex;
                nSysRunLogID = bisSysRunLog.Insert(info);
                return nSysRunLogID > 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建日志出错");
                Common.Log(ex.Message.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新运行日志
        /// </summary>
        public static void UpdateSysRunLog()
        {
            SysRunLog info = new SysRunLog();
            info.EndTime = DateTime.Now;
            info.ID = nSysRunLogID;
            new BisSysRunLog().Update(info);
        }
        #endregion 初始化配置信息

        #region 关键字分析

        #region 设置关键字分析状态全部为成功
        /// <summary>
        /// 设置关键字分析状态全部为成功
        /// </summary>
        /// <returns></returns>
        public static bool UpdateKeywordsResult()
        {
            return new BisKeywords().SetAllKeywordsResult();
        }
        #endregion

        #region 创建关键字收录分析线程

        /// <summary>
        /// 分析全部关键字
        /// </summary>
        public static void CreateAllKeywords()
        {
            Console.WriteLine("单线程分析");
            DataTable dtKeywords = new BisKeywords().GetAllList();
            CrawlerKeywords(1, dtKeywords);
            dtKeywords.Dispose();
        }

        /// <summary>
        /// 创建关键字收录分析线程
        /// </summary>
        public static void CreateKeywordsThread()
        {
            //总关键字
            int nKeyCount = new BisKeywords().CountKeywords();
            if (nKeyCount > 0)
            {
                nAllThreadCount = nKeyCount / nThreadKeyCount;
                if (nKeyCount % nThreadKeyCount > 0)
                    nAllThreadCount++;
                Console.WriteLine("共创建" + nAllThreadCount.ToString() + "个线程");
                ManualResetEvent[] mEvents = new ManualResetEvent[nAllThreadCount];
                for (int i = 0; i < nAllThreadCount; i++)
                {
                    mEvents[i] = new ManualResetEvent(false);
                    Thread tem = new Thread(new ParameterizedThreadStart(CreateCrawlerKeywords));
                    //tem.SetApartmentState(ApartmentState.STA);
                    tem.Start(new MethodParm { MethodReset = mEvents[i], Count = i });
                }
                WaitHandle.WaitAll(mEvents);
                Console.WriteLine("所有分析线程结束");
            }
            // Console.ReadLine();
        }
        #endregion

        #region 关键字收录分析

        /// <summary>
        /// 关键字收录分析
        /// </summary>
        /// <param name="objEvent"></param>
        public static void CreateCrawlerKeywords(object objEvent)
        {
            MethodParm methodParm = objEvent as MethodParm;
            int nPageIndex = methodParm.Count + 1;
            int nPageCount = 0;
            int nRecordCount = 0;
            try
            {
                Console.WriteLine(nPageIndex + "个线程");
                //关键字
                DataTable dtKeywords = new BisKeywords().GetList(string.Empty, 0, -1, -1, nPageIndex, nThreadKeyCount, ref nPageCount, ref nRecordCount);
                CrawlerKeywords(nPageIndex, dtKeywords);
                dtKeywords.Dispose();
            }
            catch (Exception ex)
            {
                Common.Log(nPageIndex.ToString(), "外围线程" + nPageIndex + "失败：" + ex.Message);
            }
            ManualResetEvent e = (ManualResetEvent)methodParm.MethodReset;
            e.Set();
        }

        /// <summary>
        /// 关键字收录分析
        /// </summary>
        public static void CrawlerKeywords(int nPageIndex, DataTable dtKeywords)
        {
            BisKeywords bisKeywords = new BisKeywords();
            try
            {
                //医院
                DataTable dtMedical = new BisMedical().GetList();
                int nMedicalID = 0;
                string strMedicalKey = string.Empty;
                int nKeywordsID = 0;
                string strKeywords = string.Empty;
                string strUrl = string.Empty;
                string strHtml = string.Empty;
                string strPageTitle = string.Empty;
                string strPageUrl = string.Empty;
                string strPageInfo = string.Empty;
                string strBaiduPageUrl = string.Empty;
                string strPageHtml = string.Empty;
                bool bIsSEO = false;
                int nRowIndex = 0;
                int nAllIndex = 0;
                DateTime dBeginTime = Convert.ToDateTime(dSysBeginTime.ToString("yyyy-MM-dd 00:00:00"));
                DateTime dEndTime = Convert.ToDateTime(dSysBeginTime.AddDays(1).ToString("yyyy-MM-dd 00:00:00"));

                BisWebSEO bisWebSEO = new BisWebSEO();
                BisNewsWeb bisNewsWeb = new BisNewsWeb();
                BisOurWeb bisOurWeb = new BisOurWeb();
                BisFilterWeb bisFilterWeb = new BisFilterWeb();
                if (dtMedical != null && dtMedical.Rows.Count > 0)
                {
                    int nCountKeyword = 0;
                    foreach (DataRow drKeywords in dtKeywords.Rows)
                    {
                        lock (LockBaiduCount)
                        {
                            CheckBaiDuCount++;
                            Console.WriteLine(DateTime.Now.ToString() + " 第" + CheckBaiDuCount + "个关键字");
                        }
                        nCountKeyword++;
                        nKeywordsID = Convert.ToInt32(drKeywords["ID"]);
                        strKeywords = drKeywords["Name"].ToString();
                        Console.WriteLine(DateTime.Now.ToString() + " 第" + nPageIndex + "个线程 第" + nCountKeyword.ToString() + "个关键字:" + strKeywords);
                        nAllIndex = 0;
                        try
                        {
                            //按配置页数抓取
                            for (int i = 0; i < nPage; i++)
                            {
                                nRowIndex = 0;
                                //do
                                //{
                                lock (LockBaidu)
                                {
                                    //if (dCheckBaiDuTime.AddSeconds(BaiduRanNum) <= DateTime.Now)
                                    //{

                                    // 抓取页面信息
                                    Console.WriteLine(DateTime.Now.ToString() + " 第" + nPageIndex + "个线程 第" + nCountKeyword.ToString() + "个关键字:" + strKeywords + " 百度第" + (i + 1) + "页");
                                    strUrl = string.Format(strBaiduUrl, Common.UrlEncode(strKeywords), i * 10);
                                    strHtml = HtmlTool.GetBaiDuHtml(strUrl);
                                    BaiduRanNum = new Random().Next(2, 6);
                                    Console.WriteLine(DateTime.Now.ToString() + " 第" + nPageIndex + "个线程 锁定:" + BaiduRanNum.ToString() + "秒");
                                    Thread.Sleep(BaiduRanNum * 1000);
                                    //dCheckBaiDuTime = DateTime.Now;
                                    //Console.WriteLine("第" + nPageIndex + "个线程 修改等待时间:" + dCheckBaiDuTime.ToString());
                                    //break;
                                    //        }
                                }
                                //    Thread.Sleep(50);
                                //} while (true);


                                if (string.IsNullOrEmpty(strHtml))
                                {
                                    lock (LockBaidu1)
                                    {
                                        Thread.Sleep(2000);
                                        strHtml = HtmlTool.GetBaiDuHtml(strUrl);
                                        if (string.IsNullOrEmpty(strHtml))
                                        {
                                            bisKeywords.SetKeywordsResult(nKeywordsID, false);
                                            continue;

                                        }
                                    }
                                }
                                //分析页面
                                mshtml.IHTMLDocument2 htmldoc = new mshtml.HTMLDocumentClass();
                                htmldoc.designMode = "on";
                                htmldoc.write(strHtml);
                                htmldoc.close();
                                HTMLDocument alldoc = htmldoc as HTMLDocument;
                                for (int j = 1; j <= 10; j++)
                                {
                                    bIsSEO = false;
                                    strPageTitle = string.Empty;
                                    strPageInfo = string.Empty;
                                    strPageUrl = string.Empty;
                                    strBaiduPageUrl = string.Empty;
                                    strPageHtml = string.Empty;
                                    nAllIndex++;
                                    nRowIndex++;
                                    try
                                    {
                                        //分析标题部分
                                        IHTMLElement eleItem = alldoc.getElementById(((i * 10) + j).ToString());
                                        if (eleItem == null)
                                            continue;
                                        mshtml.IHTMLElement2 elePanel = eleItem as IHTMLElement2;
                                        IHTMLElementCollection eleTitle = elePanel.getElementsByTagName("h3");
                                        IHTMLElementCollection eleInfo = elePanel.getElementsByTagName("div");
                                        foreach (IHTMLElement eleTem in eleTitle)
                                        {
                                            if (eleTem.className == "t")
                                            {
                                                IHTMLDOMNode firstNode = (IHTMLDOMNode)eleTem;
                                                firstNode = firstNode.firstChild;
                                                if (firstNode != null)
                                                {
                                                    IHTMLElement eleTem1 = (IHTMLElement)firstNode;
                                                    //获取标题和网址
                                                    strPageTitle = eleTem1.innerText.Trim();
                                                    strBaiduPageUrl = eleTem1.getAttribute("href").ToString();
                                                }
                                                break;
                                            }

                                        }
                                        foreach (IHTMLElement eleTem in eleInfo)
                                        {
                                            if (eleTem != null)
                                            {
                                                IHTMLDOMNode itemNode = (IHTMLDOMNode)eleTem;
                                                if (eleTem.className == "c-abstract")
                                                {
                                                    //获取介绍
                                                    if (eleTem.innerText != null)
                                                        strPageInfo = eleTem.innerText.Trim();
                                                    break;
                                                }
                                            }

                                        }

                                        if (string.IsNullOrEmpty(strBaiduPageUrl))
                                        {
                                            Common.Log(nPageIndex.ToString(), "分析SEO关键字(" + strKeywords + "第" + nAllIndex + "个)百度路径为空。");
                                            continue;
                                        }
                                        if (strBaiduPageUrl.IndexOf("http://www.baidu.com/") != 0)
                                            continue;
                                        //过滤
                                        if (strPageTitle.IndexOf("百度知道") > 0)
                                            continue;
                                        if (strPageTitle.IndexOf("搜搜问问") > 0)
                                            continue;

                                        strPageUrl = Common.GetFirstUrl(strBaiduPageUrl);
                                        if (string.IsNullOrEmpty(strPageUrl))
                                        {
                                            int randomNumber = new Random().Next(3, 6);
                                            Thread.Sleep(randomNumber * 1000);
                                            strPageUrl = Common.GetFirstUrl(strBaiduPageUrl);
                                            if (string.IsNullOrEmpty(strPageUrl))
                                            {
                                                Common.Log(strBaiduPageUrl + "获取到的路径为空");
                                                continue;
                                            }
                                        }


                                        string strTopDomain = Common.GetTopDomain(strPageUrl);
                                        string strDomain = string.Empty;
                                        if (strPageUrl.ToLower().IndexOf("http://wwww.") < 0)
                                        {
                                            strDomain = Common.GetLastDomain(strPageUrl);
                                        }

                                        //验证是否过渡域名
                                        if (bisFilterWeb.CheckFilterWeb(strTopDomain, strDomain))
                                            continue;

                                        //获取页面html
                                        strPageHtml = HtmlTool.GetHtml(strPageUrl);
                                        if (string.IsNullOrEmpty(strPageHtml))
                                            continue;

                                        if (strPageUrl.Substring(strPageUrl.Length - 1, 1) == "/")
                                        {
                                            strPageUrl = strPageUrl.Substring(0, strPageUrl.Length - 1);
                                        }

                                        //获取文章ID
                                        int nWebSeoID = 0;
                                        //匹配医院
                                        foreach (DataRow drMedical in dtMedical.Rows)
                                        {
                                            if (nSysRunIndex > 1 && !Convert.ToBoolean(drMedical["Status"]))
                                                continue;
                                            //已匹配到医院就不匹配
                                            if (bIsSEO)
                                                break;
                                            bIsSEO = false;
                                            nMedicalID = Convert.ToInt32(drMedical["ID"]);

                                            //匹配特征
                                            string[] keyList = drMedical["Keys"].ToString().Split('|');
                                            foreach (string temKey in keyList)
                                            {
                                                if (bIsSEO)
                                                    break;
                                                strMedicalKey = temKey.Trim();
                                                if (string.IsNullOrEmpty(strMedicalKey))
                                                    continue;
                                                if (strPageHtml.IndexOf(strMedicalKey) >= 0)
                                                    bIsSEO = true;
                                                ////匹配百度标题
                                                //if (strPageTitle.IndexOf(strMedicalKey) >= 0)
                                                //    bIsSEO = true;
                                                ////匹配百度介绍
                                                //if (!bIsSEO && strPageInfo.IndexOf(strMedicalKey) >= 0)
                                                //    bIsSEO = true;
                                                if (bIsSEO)
                                                {
                                                    //根据百度路径获取真实网址
                                                    //if (string.IsNullOrEmpty(strPageUrl))
                                                    //    strPageUrl = Common.GetFirstUrl(strBaiduPageUrl);
                                                    //if (string.IsNullOrEmpty(strPageUrl))
                                                    //    break;
                                                    if (nWebSeoID == 0)
                                                    {
                                                        //验证文章是否存在
                                                        nWebSeoID = bisWebSEO.GetWebSEOID(strPageUrl);
                                                        if (nWebSeoID == 0)
                                                        {
                                                            //不存在新增
                                                            WebSEO Info = new WebSEO();
                                                            Info.WebTitle = strPageTitle;
                                                            Info.WebInfo = strPageInfo;
                                                            Info.BaiduUrl = strBaiduPageUrl;
                                                            Info.WebUrl = strPageUrl;
                                                            Info.WebType = 2;
                                                            Info.CreateTime = dSysBeginTime;
                                                            if (Convert.ToBoolean(drMedical["Status"]))
                                                            {
                                                                //验证是否新闻源
                                                                if (Common.CheckNewsWeb(strPageUrl) > 0)
                                                                {
                                                                    Info.WebType = 1;
                                                                }
                                                                else
                                                                {
                                                                    //验证是否站内
                                                                    if (bisOurWeb.CheckOurWeb(strTopDomain))
                                                                    {
                                                                        Info.WebType = 3;
                                                                    }
                                                                }
                                                            }
                                                            nWebSeoID = bisWebSEO.Insert(Info);
                                                        }
                                                    }
                                                    if (nWebSeoID > 0)
                                                    {
                                                        //记录关系
                                                        DataTable dtKeysRelation = bisWebSEO.GetKeysRelation(nMedicalID, nKeywordsID, nWebSeoID, dBeginTime, dEndTime);
                                                        int nKeysRelationID = 0;
                                                        if (dtKeysRelation != null && dtKeysRelation.Rows.Count > 0)
                                                        {
                                                            nKeysRelationID = Convert.ToInt32(dtKeysRelation.Rows[0]["ID"]);
                                                            if (Convert.ToInt32(dtKeysRelation.Rows[0]["FinalPageIndex"]) < (i + 1))
                                                                bisWebSEO.UpdateKeysRelation(nKeysRelationID, (i+1));
                                                        }
                                                        else
                                                        {
                                                            nKeysRelationID = bisWebSEO.InsertKeysRelation(nMedicalID, nKeywordsID, nWebSeoID, (i + 1), nRowIndex, nAllIndex, nSysRunIndex, (i + 1), dSysBeginTime);
                                                        }
                                                        dtKeysRelation.Dispose();

                                                        //记录当天收录情况
                                                        if (nKeysRelationID > 0)
                                                        {
                                                            bisWebSEO.DeleteKeysRelationDay(nKeysRelationID, nSysRunIndex, dBeginTime, dEndTime);
                                                            bisWebSEO.InsertKeysRelationDay(nKeysRelationID, (i + 1), nRowIndex, nAllIndex, nSysRunIndex, dSysBeginTime);
                                                        }
                                                    }
                                                    Console.WriteLine(DateTime.Now.ToString() + " 第" + nPageIndex + "个线程 " + strKeywords + " 第" + (i + 1).ToString() + "页 第" + nRowIndex.ToString() + "位 收录");
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        bisKeywords.SetKeywordsResult(nKeywordsID, false);
                                        Common.Log(nPageIndex.ToString(), "分析SEO关键字(" + strKeywords + "第" + nAllIndex + "个" + strBaiduPageUrl + ")失败 " + ex.Message);
                                    }
                                }
                                htmldoc.clear();
                            }
                        }
                        catch (Exception ex)
                        {
                            bisKeywords.SetKeywordsResult(nKeywordsID, false);
                            Common.Log(nPageIndex.ToString(), "分析SEO关键字(" + strKeywords + ")失败 " + ex.Message);
                        }
                    }
                }
                dtMedical.Dispose();
            }
            catch (Exception ex)
            {
                //失败则把该线程全部关键字设置为失败
                foreach (DataRow drKeywords in dtKeywords.Rows)
                {
                    bisKeywords.SetKeywordsResult(Convert.ToInt32(drKeywords["ID"]), false);
                }
                Common.Log(nPageIndex.ToString(), "线程" + nPageIndex + "失败：" + ex.Message);
            }
        }
        #endregion

        #region 关键字分析失败的重新分析一次
        /// <summary>
        /// 关键字分析失败的重新分析一次
        /// </summary>
        public static void CrawlerErrorKeywords()
        {
            Console.WriteLine("关键字分析失败的重新分析一次");
            CheckBaiDuCount = 0;
            //关键字
            DataTable dtKeywords = new BisKeywords().GetListBySeoResult();
            if (dtKeywords != null && dtKeywords.Rows.Count > 0)
            {
                Common.Log("重新分析失败的关键字");
                CrawlerKeywords(1, dtKeywords);
            }
            dtKeywords.Dispose();
        }
        #endregion

        #endregion

        #region 关键字收录报表生成
        /// <summary>
        /// 关键字收录报表生成
        /// </summary>
        public static void CreateKeywordsReport()
        {
            try
            {
                //dSysBeginTime = Convert.ToDateTime("2013-07-25 13:03:01.093");
                //nSysRunIndex = 2;
                BisKeywordsReport bisInfo = new BisKeywordsReport();
                DateTime dBeginDay = Convert.ToDateTime(dSysBeginTime.ToString("yyyy-MM-dd"));
                DateTime dEndDay = Convert.ToDateTime(dSysBeginTime.AddDays(1).ToString("yyyy-MM-dd"));
                //获取收录信息
                DataTable dt = bisInfo.GetList(nSysRunIndex, dBeginDay, dEndDay);
                DataTable dtMedical = new BisMedical().GetList();
                DataTable dtKeywords = new BisKeywords().GetAllList();
                //删除原有的
                bisInfo.Delete(nSysRunIndex, dBeginDay, dEndDay);
                foreach (DataRow drMedical in dtMedical.Rows)
                {
                    if (nSysRunIndex > 1 && !Convert.ToBoolean(drMedical["Status"]))
                        break;
                    foreach (DataRow drKeywords in dtKeywords.Rows)
                    {
                        try
                        {
                            Console.WriteLine(drMedical["Name"].ToString() + " 关键字：" + drKeywords["Name"].ToString());
                            DataRow[] drList = dt.Select("MedicalID=" + drMedical["ID"].ToString() + " and KeywordsID=" + drKeywords["ID"].ToString());
                            KeywordsReport info = new KeywordsReport();
                            info.MedicalID = Convert.ToInt32(drMedical["ID"]);
                            info.KeywordsID = Convert.ToInt32(drKeywords["ID"]);
                            info.DayIndex = nSysRunIndex;
                            info.CreateTime = dSysBeginTime;
                            info.FirstCount = 0;
                            info.SecondCount = 0;
                            info.ThirdCount = 0;
                            info.NewsCount = 0;
                            info.LinkCount = 0;
                            foreach (DataRow dr in drList)
                            {
                                info.SEOInfo += (string.IsNullOrEmpty(info.SEOInfo) ? "" : "，") + dr["AllIndex"].ToString();
                                //统计页数
                                switch (Convert.ToInt32(dr["PageIndex"]))
                                {
                                    case 1:
                                        info.FirstCount++;
                                        break;
                                    case 2:
                                        info.SecondCount++;
                                        break;
                                    case 3:
                                        info.ThirdCount++;
                                        break;
                                }
                                //统计类型
                                switch (Convert.ToInt32(dr["WebType"]))
                                {
                                    case 1:
                                        info.NewsCount++;
                                        break;
                                    case 2:
                                        info.LinkCount++;
                                        break;
                                }
                            }
                            bisInfo.Insert(info);
                        }
                        catch (Exception ex)
                        {
                            string strErrorInfo = "\r\n生成报表错误描述："
                               + "\r\n医院：" + drMedical["Name"].ToString()
                               + "\r\n关键字:" + drKeywords["Name"].ToString()
                               + "\r\n报表时间：" + dSysBeginTime.ToString()
                               + "\r\n当天次数：" + nSysRunIndex
                               + "\r\n出错信息：" + ex.Message;
                            Common.Log(strErrorInfo);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Common.Log("关键字收录报表生成失败" + ex.Message);
            }

        }
        #endregion

        #region 关键字锁定状态分析
        /// <summary>
        /// 关键字锁定状态分析
        /// </summary>
        public static void CrawlerKeywordsSysLock()
        {
            try
            {
                BisKeywords bisKeywords = new BisKeywords();
                //将所有关键字系统锁定解除
                bisKeywords.UpdateAllSysLock(false);
                DateTime dBeginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
                DataTable dtKeywords = bisKeywords.GetKeywordsByDay(dBeginTime, dBeginTime.AddDays(1));
                if (dtKeywords != null && dtKeywords.Rows.Count > 0)
                {
                    foreach (DataRow drKeywords in dtKeywords.Rows)
                    {
                        try
                        {
                            //首页关键字收录大于等于指定值则锁定
                            if (Convert.ToInt32(drKeywords["DayCount"]) >= Convert.ToInt32(drKeywords["LockNumber"]))
                            {
                                bisKeywords.UpdateSysLock(Convert.ToInt32(drKeywords["ID"]), true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Common.Log("关键字锁定(" + drKeywords["ID"].ToString() + ")失败:" + ex.Message);
                        }
                    }
                }
                dtKeywords.Dispose();
            }
            catch (Exception ex)
            {
                Common.Log("关键字锁定状态分析失败:" + ex.Message);
            }
        }
        #endregion

        /// <summary>
        /// 关键字优化量统计
        /// </summary>
        public static void CreateKeywordsCount()
        {
            Console.WriteLine("生成关键字优化量统计");
            try
            {
                DateTime dBeginTime = DateTime.Now;
                //小于8号则从上一个月一号开始检测
                if (dBeginTime.Day < sysInfo.AccountDay.Value)
                {
                    dBeginTime = Convert.ToDateTime(dBeginTime.AddMonths(-1).ToString("yyyy-MM-01"));
                }
                else
                {
                    dBeginTime = Convert.ToDateTime(dBeginTime.ToString("yyyy-MM-01"));
                }
                DateTime dNowTime = DateTime.Now;
                TimeSpan tSpan = dNowTime - dBeginTime;
                int nDay = tSpan.Days;
                DataTable dtKeywordsList = new BisKeywords().GetAllList();
                DataTable dtUserList = new BisAdminInfo().GetAllUserList();
                BisKeywordsCount bisInfo = new BisKeywordsCount();
                DataTable dtAllCount = bisInfo.GetKeywordsCount(-1, dBeginTime, dNowTime);
                DataTable dtCount = bisInfo.GetKeywordsCount(1, dBeginTime, dNowTime);
                if (nDay > 0)
                {
                    for (int i = 0; i < nDay; i++)
                    {
                        DateTime dDayTime = dBeginTime.AddDays(i);
                        bisInfo.Delete(dDayTime);
                        foreach (DataRow drUser in dtUserList.Rows)
                        {
                            Console.WriteLine("生成" + dDayTime.ToString() + " " + drUser["Name"].ToString() + "关键字优化量统计");
                            int nUserID = Convert.ToInt32(drUser["ID"]);
                            foreach (DataRow drKeywords in dtKeywordsList.Rows)
                            {

                                KeywordsCount info = new KeywordsCount();
                                info.AdminID = nUserID;
                                info.KeywordsID = Convert.ToInt32(drKeywords["ID"]);
                                info.AllNewsCount = 0;
                                info.AllLinkCount = 0;
                                info.NewsCount = 0;
                                info.LinkCount = 0;
                                info.CreateTime = dDayTime;
                                DataRow[] drAllNewsCount = dtAllCount.Select("KID=" + drKeywords["ID"].ToString() + " and AdminID=" + nUserID.ToString() + " and day=#" + dDayTime + "# and UrlType=1");
                                if (drAllNewsCount.Length > 0)
                                    info.AllNewsCount = Convert.ToInt32(drAllNewsCount[0]["AllCount"]);
                                DataRow[] drAllLinkCount = dtAllCount.Select("KID=" + drKeywords["ID"].ToString() + " and AdminID=" + nUserID.ToString() + " and day=#" + dDayTime + "# and UrlType=2");
                                if (drAllLinkCount.Length > 0)
                                    info.AllLinkCount = Convert.ToInt32(drAllLinkCount[0]["AllCount"]);
                                DataRow[] drNewsCount = dtCount.Select("KID=" + drKeywords["ID"].ToString() + " and AdminID=" + nUserID.ToString() + " and day=#" + dDayTime + "# and UrlType=1");
                                if (drNewsCount.Length > 0)
                                    info.NewsCount = Convert.ToInt32(drNewsCount[0]["AllCount"]);
                                DataRow[] drLinkCount = dtCount.Select("KID=" + drKeywords["ID"].ToString() + " and AdminID=" + nUserID.ToString() + " and day=#" + dDayTime + "# and UrlType=2");
                                if (drLinkCount.Length > 0)
                                    info.LinkCount = Convert.ToInt32(drLinkCount[0]["AllCount"]);
                                if (info.AllNewsCount.Value > 0 || info.AllLinkCount.Value > 0 || info.NewsCount.Value > 0 || info.LinkCount.Value > 0)
                                    bisInfo.Insert(info);
                            }
                        }
                    }
                }
                dtUserList.Dispose();
                dtKeywordsList.Dispose();
            }
            catch (Exception ex)
            {
                Common.Log("关键字优化量统计失败:" + ex.Message);
            }
        }

        #region 用户文章报表生成
        /// <summary>
        /// 用户文章报表生成
        /// </summary>
        public static void CreateUserArticleReport()
        {
            try
            {
                DateTime dBeginTime = DateTime.Now;
                //小于8号则从上一个月一号开始检测
                if (dBeginTime.Day < sysInfo.AccountDay.Value)
                {
                    dBeginTime = Convert.ToDateTime(dBeginTime.AddMonths(-1).ToString("yyyy-MM-01"));
                }
                else
                {
                    dBeginTime = Convert.ToDateTime(dBeginTime.ToString("yyyy-MM-01"));
                }
                DateTime dNowTime = DateTime.Now;
                TimeSpan tSpan = dNowTime - dBeginTime;
                DataTable dtUser = new BisAdminInfo().GetAllUserList();
                BisArticleSEO bisArticleSEO = new BisArticleSEO();
                BisUserArticleReport bisInfo = new BisUserArticleReport();
                DateTime dEndTime = Common.GetAccountDay(sysInfo).AddMonths(1);
                int nDay = tSpan.Days;
                if (nDay > 0)
                {
                    for (int i = 0; i < nDay; i++)
                    {
                        DateTime dDayTime = dBeginTime.AddDays(i);
                        bisInfo.Delete(dDayTime);
                        foreach (DataRow drUser in dtUser.Rows)
                        {
                            Console.WriteLine("生成" + dDayTime.ToString() + " " + drUser["Name"].ToString() + "文章统计数据");
                            UserArticleReport Info = new UserArticleReport();
                            Info.AdminID = Convert.ToInt32(drUser["ID"]);
                            Info.OurWebCount = bisArticleSEO.CountArticle(Info.AdminID.Value, 3, -1, dDayTime, dDayTime.AddDays(1));
                            Info.OurWebUserCount = bisArticleSEO.CountArticle(Info.AdminID.Value, 3, 1, dDayTime, dDayTime.AddDays(1));
                            Info.NewsAllCount = bisArticleSEO.CountArticle(Info.AdminID.Value, 1, -1, dDayTime, dDayTime.AddDays(1));
                            Info.NewsCount1 = 0;
                            Info.NewsAllSEOCount = 0;
                            Info.NewsCount2 = 0;
                            Info.LinkAllCount = bisArticleSEO.CountArticle(Info.AdminID.Value, 2, -1, dDayTime, dDayTime.AddDays(1));
                            Info.LinkAllSEOCount = 0;
                            Info.LinkCount1 = 0;
                            Info.LinkCount2 = 0;
                            Info.CreateTime = dDayTime;
                            DataTable dtArticle = bisArticleSEO.GetUserArticleList(Info.AdminID.Value, true, dDayTime, dDayTime.AddDays(1));
                            foreach (DataRow drArticle in dtArticle.Rows)
                            {
                                int nUrlType = Convert.ToInt32(drArticle["UrlType"]);
                                int nRe = 0;
                                int nRe1 = 0;
                                //分析关键字1
                                if (drArticle["KeywordsID"] != DBNull.Value)
                                    nRe = bisArticleSEO.CountArticleReport(Convert.ToInt32(drArticle["WebSEOID"]), Convert.ToInt32(drArticle["KeywordsID"]), dDayTime.AddDays(1), dEndTime, nBaiduDay);
                                if (nRe > 0)
                                {
                                    switch (nUrlType)
                                    {
                                        case 1:
                                            if (nRe == 1)
                                            {
                                                Info.NewsCount1 += 1;
                                            }
                                            else if (nRe == 2)
                                            {
                                                Info.NewsCount2 += 1;
                                            }
                                            break;
                                        case 2:
                                            if (nRe == 1)
                                            {
                                                Info.LinkCount1 += 1;
                                            }
                                            else if (nRe == 2)
                                            {
                                                Info.LinkCount2 += 1;
                                            }
                                            break;
                                    }
                                }

                                //分析关键字2
                                if (drArticle["KeywordsID1"] != DBNull.Value)
                                    nRe1 = bisArticleSEO.CountArticleReport(Convert.ToInt32(drArticle["WebSEOID"]), Convert.ToInt32(drArticle["KeywordsID1"]), dDayTime.AddDays(1), dEndTime, nBaiduDay);
                                if (nRe1 > 0)
                                {
                                    switch (nUrlType)
                                    {
                                        case 1:
                                            if (nRe1 == 1)
                                            {
                                                Info.NewsCount1 += 1;
                                            }
                                            else if (nRe1 == 2)
                                            {
                                                Info.NewsCount2 += 1;
                                            }
                                            break;
                                        case 2:
                                            if (nRe1 == 1)
                                            {
                                                Info.LinkCount1 += 1;
                                            }
                                            else if (nRe1 == 2)
                                            {
                                                Info.LinkCount2 += 1;
                                            }
                                            break;
                                    }
                                }

                                if (nRe > 0 || nRe1 > 0)
                                {
                                    switch (nUrlType)
                                    {
                                        case 1:
                                            Info.NewsAllSEOCount += 1;
                                            break;
                                        case 2:
                                            Info.LinkAllSEOCount += 1;
                                            break;
                                    }
                                }
                            }
                            dtArticle.Dispose();
                            bisInfo.Insert(Info);
                        }
                    }
                }
                dtUser.Dispose();
            }
            catch (Exception ex)
            {
                Common.Log("用户文章报表生成失败:" + ex.Message);
            }
        }
        #endregion

        #region 网编文章收录分析
        /// <summary>
        /// 网编文章收录分析
        /// </summary>
        public static void CrawlerArticle()
        {
            try
            {
                BisWebSEO bisWebSEO = new BisWebSEO();
                BisArticleSEO bisArticleSEO = new BisArticleSEO();
                DateTime dBeginTime = DateTime.Now;
                DateTime dArticleBeginTime = DateTime.Now;
                DateTime dArticleEndTime = DateTime.Now;
                //小于8号则从上一个月一号开始检测
                if (dCheckBaiDuTime.Day < sysInfo.AccountDay.Value)
                {
                    dArticleBeginTime = Convert.ToDateTime(dArticleBeginTime.AddMonths(-1).ToString("yyyy-MM-01"));
                    dArticleEndTime = Convert.ToDateTime(dBeginTime.ToString("yyyy-MM-" + sysInfo.AccountDay.Value.ToString("00")));
                }
                else
                {
                    dArticleBeginTime = Convert.ToDateTime(dArticleBeginTime.ToString("yyyy-MM-01"));
                    dArticleEndTime = Convert.ToDateTime(dBeginTime.AddMonths(1).ToString("yyyy-MM-" + sysInfo.AccountDay.Value.ToString("00")));
                }
                //获取状态还是没收录过的文章
                DataTable dtArticleSeo = new BisArticleSEO().GetCrawlerArticleList(false, dArticleBeginTime, dArticleEndTime);
                int nWebSEOID = 0;
                int nKeywordsID = 0;
                int nKeywordsID1 = 0;
                foreach (DataRow drArticleSeo in dtArticleSeo.Rows)
                {
                    try
                    {
                        Console.WriteLine("检测文章网址：" + drArticleSeo["ArticleUrl"].ToString());
                        if (Convert.ToInt32(drArticleSeo["UrlType"]) == 3)
                        {
                            //站内类型则只验证是否收录
                            if (CheckBaiDu(drArticleSeo["ArticleUrl"].ToString()))
                                bisArticleSEO.UpdateStatus(Convert.ToInt32(drArticleSeo["ID"]), true);
                        }
                        else
                        {
                            //验证百度收录关系
                            dBeginTime = Convert.ToDateTime(Convert.ToDateTime(drArticleSeo["CreateTime"]).AddDays(1).ToString("yyyy-MM-dd"));
                            int.TryParse(drArticleSeo["KeywordsID"].ToString(), out nKeywordsID);
                            int.TryParse(drArticleSeo["KeywordsID1"].ToString(), out nKeywordsID1);
                            bisArticleSEO.CheckBaiduSEO(Convert.ToInt32(drArticleSeo["ID"]), Convert.ToInt32(drArticleSeo["SortID"]), nKeywordsID, nKeywordsID1, drArticleSeo["ArticleUrl"].ToString(), dBeginTime, ref nWebSEOID);
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.Log("检测文章网址(" + drArticleSeo["ArticleUrl"].ToString() + ")失败:" + ex.Message);
                    }
                }
                dtArticleSeo.Dispose();
            }
            catch (Exception ex)
            {
                Common.Log("网编文章收录分析:" + ex.Message);
            }
        }
        #endregion

        #region 网编文章提成分析

        /// <summary>
        /// 每月8日分析提成
        /// </summary>
        public static void MainCrawlerMemony()
        {
            //每月8日分析上一个月的提成
            if (CheckAccountDay())
            {
                Console.WriteLine("网编文章提成月结算");
                DateTime dBeginTime = Convert.ToDateTime(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01"));
                DateTime dEndTime = dBeginTime.AddMonths(1);
                CrawlerMemony(dBeginTime, dEndTime);
            }
        }

        /// <summary>
        /// 定时分析提成
        /// </summary>
        public static void CrawlerMemonyByDay()
        {
            Console.WriteLine("网编文章提成分析");
            DateTime dBeginTime = DateTime.Now;
            DateTime dEndTime = DateTime.Now.AddDays(-1);
            //小于8号则从上一个月一号开始检测
            if (dBeginTime.Day < sysInfo.AccountDay.Value)
            {
                dBeginTime = Convert.ToDateTime(dBeginTime.AddMonths(-1).ToString("yyyy-MM-01"));
            }
            else
            {
                dBeginTime = Convert.ToDateTime(dBeginTime.ToString("yyyy-MM-01"));
            }
            CrawlerMemony(dBeginTime, dEndTime);
        }

        /// <summary>
        /// 网编文章提成分析
        /// </summary>
        /// <param name="dArticleBeginTime">文章录入开始时间</param>
        /// <param name="dArticleEndTime">文章录入结束时间</param>
        /// <param name="dSysTime">系统结算时间</param>
        public static void CrawlerMemony(DateTime dArticleBeginTime, DateTime dArticleEndTime)
        {
            try
            {
                BisArticleSEO bisArticleSEO = new BisArticleSEO();
                //获取已收录的文章
                DataTable dtArticleSeo = new BisArticleSEO().GetList(true, dArticleBeginTime, dArticleEndTime);
                int nKeywordsID = 0;
                int nKeywordsID1 = 0;
                string strTopDomain = string.Empty;
                string strDomain = string.Empty;
                DateTime dEndCheckTime = DateTime.Now;
                foreach (DataRow drArticleSeo in dtArticleSeo.Rows)
                {
                    strTopDomain = string.Empty;
                    strDomain = string.Empty;
                    try
                    {
                        Console.WriteLine("检测文章：" + drArticleSeo["AritcleTitle"].ToString());
                        if (Convert.ToInt32(drArticleSeo["UrlType"]) == 3)
                        {
                            //站内类型只要有收录就提成
                            bisArticleSEO.UpdateIsPass(Convert.ToInt32(drArticleSeo["ID"]), true, sysInfo.OurWebMomey.Value, dArticleEndTime);
                        }
                        else
                        {
                            int.TryParse(drArticleSeo["KeywordsID"].ToString(), out nKeywordsID);
                            int.TryParse(drArticleSeo["KeywordsID1"].ToString(), out nKeywordsID1);
                            strTopDomain = Common.GetTopDomain(drArticleSeo["ArticleUrl"].ToString());
                            strDomain = drArticleSeo["ArticleUrl"].ToString().Trim();
                            if (strDomain.ToLower().IndexOf("http://www.") < 0)
                            {
                                strDomain = Common.GetLastDomain(strDomain);
                            }
                            else
                            {
                                strDomain = string.Empty;
                            }
                            dEndCheckTime = Convert.ToDateTime(Convert.ToDateTime(drArticleSeo["CreateTime"]).AddMonths(1).ToString("yyyy-MM-" + sysInfo.AccountDay.Value.ToString("00")));
                            //非站内类型提成计算
                            bisArticleSEO.CountArticleMomey(
                                Convert.ToInt32(drArticleSeo["ID"]),
                                Convert.ToInt32(drArticleSeo["UrlType"]),
                                Convert.ToInt32(drArticleSeo["WebSEOID"]),
                                strTopDomain,
                                strDomain,
                                nKeywordsID,
                                nKeywordsID1,
                                Convert.ToDateTime(Convert.ToDateTime(drArticleSeo["CreateTime"]).AddDays(1).ToString("yyyy-MM-dd 00:00:00")),
                                dEndCheckTime,
                                sysInfo);
                        }
                    }
                    catch (Exception ex)
                    {
                        Common.Log("网编文章提成分析(" + drArticleSeo["AritcleTitle"].ToString() + ")失败:" + ex.Message);
                    }
                }
                dtArticleSeo.Dispose();
                //打上结算标记
                // bisArticleSEO.UpdateIsAccount();
            }
            catch (Exception ex)
            {
                Common.Log("网编文章提成分析:" + ex.Message);
            }

        }
        #endregion

        #region 共用方法
        /// <summary>
        /// 更新所有收录网址Url
        /// </summary>
        public static void UpdateWebSEOUrl()
        {
            BisWebSEO bisInfo = new BisWebSEO();
            BisNewsWeb bisNewsWeb = new BisNewsWeb();
            BisOurWeb bisOurWeb = new BisOurWeb();
            DataTable dt = bisInfo.GetAllList();
            int nCount = 0;
            foreach (DataRow dr in dt.Rows)
            {
                nCount++;
                Console.WriteLine(nCount + " " + dr["ID"].ToString());
                string strPageUrl = dr["WebUrl"].ToString();
                //去"/"
                if (strPageUrl.Substring(strPageUrl.Length - 1, 1) == "/")
                {
                    strPageUrl = strPageUrl.Substring(0, strPageUrl.Length - 1);
                    bool bResult = bisInfo.UpdateWebUrl(Convert.ToInt32(dr["ID"]), strPageUrl);
                    Console.WriteLine(" 存在" + (bResult ? "成功" : "失败"));
                }
                else
                {
                    Console.WriteLine("不存在");
                }


            }
        }
        /// <summary>
        /// 更新所有收录网址的类型
        /// </summary>
        public static void UpdateWebSEOType()
        {
            BisWebSEO bisInfo = new BisWebSEO();
            BisNewsWeb bisNewsWeb = new BisNewsWeb();
            BisOurWeb bisOurWeb = new BisOurWeb();
            DataTable dt = bisInfo.GetAllList();
            int nCount = 0;
            foreach (DataRow dr in dt.Rows)
            {
                nCount++;
                Console.WriteLine(nCount + " " + dr["ID"].ToString());
                int nWebType = 2;
                string strTopDomain = Common.GetTopDomain(dr["WebUrl"].ToString());
                if (bisNewsWeb.CheckNewsWeb(strTopDomain))
                {
                    nWebType = 1;
                }
                else
                {
                    //验证是否站内
                    if (bisOurWeb.CheckOurWeb(strTopDomain))
                    {
                        nWebType = 3;
                    }
                }
                bool bResult = bisInfo.UpdateWebType(Convert.ToInt32(dr["ID"]), nWebType);
                Console.WriteLine((nWebType == 1 ? "新闻源" : (nWebType == 2 ? "外链" : "站内")) + " " + (bResult ? "成功" : "失败"));
            }
        }

        /// <summary>
        /// 验证是否结算日
        /// </summary>
        /// <returns></returns>
        public static bool CheckAccountDay()
        {
            if (nSysRunIndex == 1)
            {
                string strAccountDay = Common.GetAccountDay(sysInfo).ToString("yyyy-MM-dd");
                string strNowDay = DateTime.Now.ToString("yyyy-MM-dd");
                if (DateTime.Now.Day == sysInfo.AccountDay.Value)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 验证网址百度是否收入
        /// </summary>
        /// <param name="strUrl"></param>
        public static bool CheckBaiDu(string strUrl)
        {
            BaiduRanNum = new Random().Next(2, 5);
            Thread.Sleep(BaiduRanNum * 1000);
            strUrl = string.Format(strBaiduUrl, Common.UrlEncode(strUrl), 1);
            string strHtml = HtmlTool.GetBaiDuHtml(strUrl);
            if (!string.IsNullOrEmpty(strHtml))
            {
                mshtml.IHTMLDocument2 htmldoc = new mshtml.HTMLDocumentClass();
                htmldoc.designMode = "on";
                htmldoc.write(strHtml);
                htmldoc.close();
                HTMLDocument alldoc = htmldoc as HTMLDocument;
                IHTMLElement eleItem = alldoc.getElementById("content_left");
                if (eleItem != null)
                {
                    IHTMLDOMNode firstNode = (IHTMLDOMNode)eleItem;
                    firstNode = firstNode.firstChild;
                    if (firstNode != null)
                    {
                        eleItem = (IHTMLElement)firstNode;
                        if (eleItem.getAttribute("id") != null && eleItem.getAttribute("id").ToString() == "1")
                        {
                            htmldoc.clear();
                            return true;
                        }
                    }
                }
                htmldoc.clear();
            }
            return false;
        }

        #endregion
    }
}
